package week10.homework;

public class DVDlist {
    private  DVDNode list;

    public DVDlist () {
        list=null;
    }
    int a=1;
    public void add(DVD mag)
    {
        DVDNode node = new DVDNode(mag);
        DVDNode current;

        if (list == null)
            list = node;
        else
        {
            current = list;
            while (current.next != null)
                current = current.next;
            current.next = node;

        }
    }
    public void insert(int index, DVD newMagazine){
        DVDNode node1 =new DVDNode(newMagazine);
        DVDNode current2=node1;
        DVDNode current1 =list;
        DVDNode current3=list;
        DVDNode current4=list;
        DVDNode current6=list;
        while (current1.next!=null)
        {a++;current1=current1.next;}
        if (index==1)
        {node1.next=current4;
            list=node1;}
        if (1<index&index<a+1) {
            for (int a = 2; a < index; a++)
            {
                if (current4.next!=null)
                    current4= current4.next;
            }
            for(int a=2;a<index+1;a++){
                if(current6.next!=null)
                    current6= current6.next; }
            DVDNode current5=current4;
            current5.next=node1;
            node1.next=current6; }
        if(index==a+1) {
            for (int an = 0; an < a; an++)
            {  if(current3.next!=null)
                current3=current3.next; }
            current3.next=node1; }
    }

    public String toString()
    {
        String result = "";

        DVDNode current = list;

        while (current != null)
        {
            result += current.magazine + "\n";
            current = current.next;
        }

        return result;
    }

    public void selectionSort(){

        DVDNode tail=null;
        DVDNode cur = list;

        DVDNode smallPre = null;
        DVDNode small=null;

        while(cur !=null){
            small=cur;
            smallPre=getSmallestPreNode(cur);
            if(smallPre !=null){
                small = smallPre.next;
                smallPre.next=small.next;  //删除掉
            }
            cur=cur==small ? cur.next :cur;   //看看当前的值是不是最小的那个
            if(tail==null){
                list=small;  //第一个
            }else {
                tail.next=small;
            }
            tail=small;  //更新下尾部
        }

    }

    public static DVDNode getSmallestPreNode(DVDNode head){

        DVDNode smallPre=null;
        DVDNode small=head;
        DVDNode pre = head;
        DVDNode cur=head.next;

        while(cur!=null){
            if(cur.toString().compareTo(small.toString()) <0){
                smallPre=pre;
                small=cur;
            }
            pre=cur;
            cur=cur.next;
        }
        return smallPre;
    }


    private class DVDNode
    {
        public DVD magazine;
        public DVDNode next;

        //--------------------------------------------------------------
        //  Sets up the node
        //--------------------------------------------------------------
        public DVDNode(DVD mag)
        {
            magazine = mag;
            next = null;
        }
    }}
